//
//  CompanyInfoAddItemViewController.m
//  Btpin
//
//  Created by Vic on 2019/4/13.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "CompanyInfoAddItemViewController.h"
#import "CompanyCreatInfoCell.h"
#import <Photos/Photos.h>
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
#import "MineInfoEditViewController.h"
#import "SelcetCityViewController.h"
#import "MineInfoEditViewController.h"
#import "PMineResumeSelfDescriptionViewController.h"
#import "CompanyJobtagViewController.h"
#import "AppDelegate.h"

@interface CompanyInfoAddItemViewController ()<UITableViewDelegate, UITableViewDataSource, UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, PSSingleRowViewDelegate, SelcetCityViewControllerDelegate, MineInfoEditViewControllerDelegate, PMineResumeSelfDescriptionViewControllerDelegate, CompanyJobtagViewControllerDelegate>
{
    NSMutableArray *dataArray;
    
    PSSingleRowView *headerPickerView;
    UITableView *infoTableView;
    UIView *headerView1;
    UIView *headerView2;
    
    UIView *footerView;
    UIButton *upCerBtn;
    
    NSString *logoURL;
    NSString *cerURL;
    UIImageView *logoImgView;
    NSArray *JobtagInfoArray;
    
    NSInteger picFlag; // 1:logo 2:营业执照
    
    NSMutableArray *modelTags;
}
@property (nonatomic, strong) PSSingleRowView *naturePickerView;
@property (nonatomic, strong) PSSingleRowView *financePickerView;
@property (nonatomic, strong) PSSingleRowView *tradePickerView;
@property (nonatomic, strong) PSSingleRowView *scalePickerView;
@end

@implementation CompanyInfoAddItemViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (headerPickerView) {
        headerPickerView.hidden = YES;
    }
    if (self.naturePickerView) {
        self.naturePickerView.hidden = YES;
    }
    if (self.financePickerView) {
        self.financePickerView.hidden = YES;
    }
    if (self.tradePickerView) {
        self.tradePickerView.hidden = YES;
    }
    if (self.scalePickerView) {
        self.scalePickerView.hidden = YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"新增企业";
    logoURL = @"";
    cerURL = @"";
    modelTags = [NSMutableArray arrayWithCapacity:0];
    
    [self loadDataInfo];
    [self loadDataLocation];
    [self initUI];
}
- (void)okBtnTap {
    if (logoURL && logoURL.length) {
    } else {
        SVPHUDError(CompanyLogoAlertText);
        return;
    }
    if (cerURL && cerURL.length) {
    } else {
        SVPHUDError(@"请上传公司营业执照");
        return;
    }
    if (JobtagInfoArray.count) {
    } else {
        SVPHUDError(@"请选择公司福利");
        return;
    }
    
    for (NSInteger i = 0; i < [dataArray[0] count]; i++) {
        PublicCategoryElementModel *model = dataArray[0][i];
        if (model.detail && model.detail.length) {
        } else {
            switch (i) {
                case 0: {
                    SVPHUDError(CompanyAlertText);
                } break;
                case 1: {
                    SVPHUDError(CompanyNameForShortAlertText);
                } break;
                case 2: {
                    SVPHUDError(CompanyTypeAlertText);
                } break;
                case 3: {
                    SVPHUDError(CompanyFinanceAlertText);
                } break;
                case 4: {
                    SVPHUDError(CompanySelectAddressAlertText);
                } break;
                case 5: {
                    SVPHUDError(CompanyTradeAlertText);
                } break;
                case 6: {
                    SVPHUDError(CompanyScaleAlertText);
                } break;
                case 7: {
                    SVPHUDError(CompanyIntroduceAlertText);
                } break;
                case 8: {
                    SVPHUDError(CompanyJobtagAlertText);
                } break;
                default:
                    break;
            }
            return;
        }
    }
    for (NSInteger i = 0; i < [dataArray[1] count]; i++) {
        PublicCategoryElementModel *model = dataArray[1][i];
        if (model.detail && model.detail.length) {
        } else {
            switch (i) {
                case 0: {
                    SVPHUDError(CompanyLandlineAlertText);
                    return;
                } break;
                case 1: {
                    SVPHUDError(CompanyAddressAlertText);
                    return;
                } break;
                default:
                    break;
            }
        }
    }
    [self updateInfo];
}
- (void)updateInfo {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:logoURL forKey:@"logo"];
//    PublicCategoryElementModel *model10 = dataArray[0][0];
//    [dic setObject:model10.detail forKey:@"full_name"];
    PublicCategoryElementModel *model11 = dataArray[0][1];
    [dic setObject:model11.detail forKey:@"short_name"];
    PublicCategoryElementModel *model12 = dataArray[0][2];
    [dic setObject:model12.id forKey:@"nature"];
    PublicCategoryElementModel *model13 = dataArray[0][3];
    [dic setObject:model13.id forKey:@"finance"];
    PublicCategoryElementModel *model14 = dataArray[0][4];
    [dic setObject:model14.id forKey:@"district"];
    PublicCategoryElementModel *model15 = dataArray[0][5];
    [dic setObject:model15.id forKey:@"trade"];
    PublicCategoryElementModel *model16 = dataArray[0][6];
    [dic setObject:model16.id forKey:@"scale"];
    PublicCategoryElementModel *model17 = dataArray[0][7];
    [dic setObject:model17.detail forKey:@"contents"];
    [dic setObject:JobtagInfoArray forKey:@"tags"];
    PublicCategoryElementModel *model20 = dataArray[1][0];
    [dic setObject:model20.detail forKey:@"landline_tel"];
    PublicCategoryElementModel *model21 = dataArray[1][1];
    [dic setObject:model21.detail forKey:@"address"];
    PublicCategoryElementModel *model22 = dataArray[1][2];
    [dic setObject:((model22.detail && model22.detail.length)?model22.detail:@"") forKey:@"website"];
    PublicCategoryElementModel *model23 = dataArray[1][3];
    [dic setObject:((model23.detail && model23.detail.length)?model23.detail:@"") forKey:@"email"];
    [dic setObject:@"" forKey:@"map_x"];
    [dic setObject:@"" forKey:@"map_y"];
    [dic setObject:@"" forKey:@"map_zoom"];
    [dic setObject:cerURL forKey:@"certificate_img"];
    
    [BaseNetManager requestWithMethod:POST path:Company_SaveBaseInfo_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(responseObject[@"msg"]);
            
            [DataToolsControl getMyBaseInfoForCompany:^(BOOL requestFlag) {
            }];
            
            [self performSelector:@selector(delayClick) withObject:nil afterDelay:0.5];
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)delayClick {
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)loadDataInfo {
    self.naturePickerView = [[PSSingleRowView alloc] initWithData:[DataToolsControl sharedManager].allCategoryInfoModel.companyTypeArray target:self];
    self.naturePickerView.hidden = YES;
    self.financePickerView = [[PSSingleRowView alloc] initWithData:[DataToolsControl sharedManager].allCategoryInfoModel.financeArray target:self];
    self.financePickerView.hidden = YES;
    self.tradePickerView = [[PSSingleRowView alloc] initWithData:[DataToolsControl sharedManager].allCategoryInfoModel.tradeArray target:self];
    self.tradePickerView.hidden = YES;
    self.scalePickerView = [[PSSingleRowView alloc] initWithData:[DataToolsControl sharedManager].allCategoryInfoModel.scaleArray target:self];
    self.scalePickerView.hidden = YES;
}
- (void)upCerBtnTap {
    picFlag = 2;
    headerPickerView.hidden = NO;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [dataArray[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CompanyCreatInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:isCellID];
    if (!cell) {
        cell = [[CompanyCreatInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:isCellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    PublicCategoryElementModel *model = dataArray[indexPath.section][indexPath.row];
    [cell reloadDataModel:model];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section) {
        switch (indexPath.row) {
            case 0: { // 企业座机
                [self pushInfoEditVC:InfoEditTypeOfCompanyLandline];
            } break;
            case 1: { // 办公地址
                [self pushInfoEditVC:InfoEditTypeOfCompanyAddress];
            } break;
            case 2: { // 企业网站
                [self pushInfoEditVC:InfoEditTypeOfCompanyWebURL];
            } break;
            case 3: { // 企业邮箱
                [self pushInfoEditVC:InfoEditTypeOfEmail];
            } break;
            default:
                break;
        }
    } else {
        switch (indexPath.row) {
            case 0: { // 企业名称
                [self pushInfoEditVC:InfoEditTypeOfCompanyName];
            } break;
            case 1: { // 企业简称
                [self pushInfoEditVC:InfoEditTypeOfCompanyNameForShort];
            } break;
            case 2: { // 企业性质
                self.naturePickerView.hidden = NO;
            } break;
            case 3: { // 融资阶段
                self.financePickerView.hidden = NO;
            } break;
            case 4: { // 所在地区
                SelcetCityViewController *SCVC = [[SelcetCityViewController alloc] init];
                SCVC.delegate = self;
                [self.navigationController pushViewController:SCVC animated:YES];
            } break;
            case 5: { // 所属行业
                self.tradePickerView.hidden = NO;
            } break;
            case 6: { // 企业规模
                self.scalePickerView.hidden = NO;
            } break;
            case 7: { // 企业介绍
                PMineResumeSelfDescriptionViewController *PMRSDVC = [[PMineResumeSelfDescriptionViewController alloc] init];
                PMRSDVC.limitTextMaxNumber = 1000;
                PMRSDVC.titleStr = @"企业介绍";
                PMRSDVC.delegate = self;
                [self.navigationController pushViewController:PMRSDVC animated:YES];
            } break;
            case 8: { // 企业福利
                CompanyJobtagViewController *CJVC = [[CompanyJobtagViewController alloc] init];
                CJVC.tagArr = [NSMutableArray arrayWithCapacity:0];
                if (modelTags && modelTags.count) {
                    for (TagModel *mmodel in modelTags) {
                        [CJVC.tagArr addObject:[mmodel copy]];
                    }
                }
                CJVC.delegate = self;
                [self.navigationController pushViewController:CJVC animated:YES];
            } break;
            default:
                break;
        }
    }
}
- (void)companyJobtagViewControllerBackInfo:(NSString *)infoStr infoArray:(NSArray *)infoArray modelArray:(nonnull NSArray *)modelArray {
    if (modelArray && modelArray.count) {
        [modelTags removeAllObjects];
        for (TagModel *mmodel in modelArray) {
            [modelTags addObject:[mmodel copy]];
        }
    }
    JobtagInfoArray = infoArray;
    PublicCategoryElementModel *tempModel = dataArray[0][8];
    tempModel.detail = infoStr;
    [dataArray[0] replaceObjectAtIndex:8 withObject:tempModel];
    [infoTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:8 inSection:0], nil] withRowAnimation:UITableViewRowAnimationNone];
}
- (void)pMineResumeSelfDescriptionViewControllerBack:(NSString *)info {
    PublicCategoryElementModel *tempModel = dataArray[0][7];
    tempModel.detail = info;
    [dataArray[0] replaceObjectAtIndex:7 withObject:tempModel];
    [infoTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:7 inSection:0], nil] withRowAnimation:UITableViewRowAnimationNone];
}
- (void)pushInfoEditVC:(InfoEditType)type {
    MineInfoEditViewController *MIEVC = [[MineInfoEditViewController alloc] init];
    MIEVC.type = type;
    MIEVC.delegate = self;
    [self.navigationController pushViewController:MIEVC animated:YES];
}
- (void)mineInfoEditViewController:(NSString *)info type:(InfoEditType)type {
    switch (type) {
        case InfoEditTypeOfCompanyName: {
            PublicCategoryElementModel *tempModel = dataArray[0][0];
            tempModel.detail = info;
            [dataArray[0] replaceObjectAtIndex:0 withObject:tempModel];
            [infoTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:0], nil] withRowAnimation:UITableViewRowAnimationNone];
        } break;
        case InfoEditTypeOfCompanyNameForShort: {
            PublicCategoryElementModel *tempModel = dataArray[0][1];
            tempModel.detail = info;
            [dataArray[0] replaceObjectAtIndex:1 withObject:tempModel];
            [infoTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:1 inSection:0], nil] withRowAnimation:UITableViewRowAnimationNone];
        } break;
        case InfoEditTypeOfCompanyLandline: {
            PublicCategoryElementModel *tempModel = dataArray[1][0];
            tempModel.detail = info;
            [dataArray[1] replaceObjectAtIndex:0 withObject:tempModel];
            [infoTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:1], nil] withRowAnimation:UITableViewRowAnimationNone];
        } break;
        case InfoEditTypeOfCompanyAddress: {
            PublicCategoryElementModel *tempModel = dataArray[1][1];
            tempModel.detail = info;
            [dataArray[1] replaceObjectAtIndex:1 withObject:tempModel];
            [infoTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:1 inSection:1], nil] withRowAnimation:UITableViewRowAnimationNone];
        } break;
        case InfoEditTypeOfCompanyWebURL: {
            PublicCategoryElementModel *tempModel = dataArray[1][2];
            tempModel.detail = info;
            [dataArray[1] replaceObjectAtIndex:2 withObject:tempModel];
            [infoTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:2 inSection:1], nil] withRowAnimation:UITableViewRowAnimationNone];
        } break;
        case InfoEditTypeOfEmail: {
            PublicCategoryElementModel *tempModel = dataArray[1][3];
            tempModel.detail = info;
            [dataArray[1] replaceObjectAtIndex:3 withObject:tempModel];
            [infoTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:3 inSection:1], nil] withRowAnimation:UITableViewRowAnimationNone];
        } break;
        default:
            break;
    }
}
- (void)selcetCityViewControllerInfo:(NSDictionary *)dic {
    PublicCategoryElementModel *tempModel = dataArray[0][4];
    tempModel.detail = [NSString stringWithFormat:@"%@-%@-%@", dic[@"province"], dic[@"city"], dic[@"area"]];
    tempModel.id = [NSString stringWithFormat:@"%@.%@.%@", dic[@"provinceid"], dic[@"cityid"], dic[@"areaid"]];
    [dataArray[0] replaceObjectAtIndex:4 withObject:tempModel];
    [infoTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:4 inSection:0], nil] withRowAnimation:UITableViewRowAnimationNone];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section) {
        return headerView2;
    } else {
        return headerView1;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section) {
        return 10*Scale;
    }
    return 130*Scale;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section) {
        return 220*Scale;
    }
    return 0.00001;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section) {
        return footerView;
    }
    return nil;
}

- (void)psSingleRowViewCancel:(PSSingleRowView *)psSingleRowView {
    psSingleRowView.hidden = YES;
}
- (void)psSingleRowViewOk:(PublicCategoryElementModel *)model PSSingleRowView:(PSSingleRowView *)psSingleRowView {
    psSingleRowView.hidden = YES;
    if (psSingleRowView == self.naturePickerView) {
        PublicCategoryElementModel *tempModel = dataArray[0][2];
        tempModel.detail = model.name;
        tempModel.id = model.id;
        [dataArray[0] replaceObjectAtIndex:2 withObject:tempModel];
        [infoTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:2 inSection:0], nil] withRowAnimation:UITableViewRowAnimationNone];
    } else if (psSingleRowView == self.financePickerView) {
        PublicCategoryElementModel *tempModel = dataArray[0][3];
        tempModel.detail = model.name;
        tempModel.id = model.id;
        [dataArray[0] replaceObjectAtIndex:3 withObject:tempModel];
        [infoTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:3 inSection:0], nil] withRowAnimation:UITableViewRowAnimationNone];
    } else if (psSingleRowView == self.tradePickerView) {
        PublicCategoryElementModel *tempModel = dataArray[0][5];
        tempModel.detail = model.name;
        tempModel.id = model.id;
        [dataArray[0] replaceObjectAtIndex:5 withObject:tempModel];
        [infoTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:5 inSection:0], nil] withRowAnimation:UITableViewRowAnimationNone];
    } else if (psSingleRowView == self.scalePickerView) {
        PublicCategoryElementModel *tempModel = dataArray[0][6];
        tempModel.detail = model.name;
        tempModel.id = model.id;
        [dataArray[0] replaceObjectAtIndex:6 withObject:tempModel];
        [infoTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:6 inSection:0], nil] withRowAnimation:UITableViewRowAnimationNone];
    } else if (psSingleRowView == headerPickerView) {
        if ([model.id integerValue] == 1) { // 相册
            [self openPhotoLibrary];
        } else { // 拍摄
            [self openCamera];
        }
    }
}
- (void)loadDataLocation {
    NSMutableArray *headerArray = [NSMutableArray arrayWithCapacity:0];
    PublicCategoryElementModel *model111 = [[PublicCategoryElementModel alloc] init];
    [model111 setValuesForKeysWithDictionary:@{@"id":@"0", @"name":@"拍摄"}];
    [headerArray addObject:model111];
    PublicCategoryElementModel *model222 = [[PublicCategoryElementModel alloc] init];
    [model222 setValuesForKeysWithDictionary:@{@"id":@"1", @"name":@"相册"}];
    [headerArray addObject:model222];
    headerPickerView = [[PSSingleRowView alloc] initWithData:headerArray target:self];
    headerPickerView.hidden = YES;
    
    if (([DataToolsControl sharedManager].companyBaseInfoModel && [DataToolsControl sharedManager].companyBaseInfoModel.tag.count)) {
        for (TagModel *model in [DataToolsControl sharedManager].companyBaseInfoModel.tag) {
            [modelTags addObject:[model copy]];
        }
    }
    
    dataArray = [NSMutableArray arrayWithCapacity:0];
    NSMutableArray *arr1 = [NSMutableArray arrayWithCapacity:0];
    PublicCategoryElementModel *model1 = [[PublicCategoryElementModel alloc] init];
    model1.name = @"企业名称";
    [arr1 addObject:model1];
    PublicCategoryElementModel *model2 = [[PublicCategoryElementModel alloc] init];
    model2.name = @"企业简称";
    [arr1 addObject:model2];
    PublicCategoryElementModel *model3 = [[PublicCategoryElementModel alloc] init];
    model3.name = @"企业性质";
    [arr1 addObject:model3];
    PublicCategoryElementModel *model4 = [[PublicCategoryElementModel alloc] init];
    model4.name = @"融资阶段";
    [arr1 addObject:model4];
    PublicCategoryElementModel *model5 = [[PublicCategoryElementModel alloc] init];
    model5.name = @"所在地区";
    [arr1 addObject:model5];
    PublicCategoryElementModel *model6 = [[PublicCategoryElementModel alloc] init];
    model6.name = @"所属行业";
    [arr1 addObject:model6];
    PublicCategoryElementModel *model7 = [[PublicCategoryElementModel alloc] init];
    model7.name = @"企业规模";
    [arr1 addObject:model7];
    PublicCategoryElementModel *model8 = [[PublicCategoryElementModel alloc] init];
    model8.name = @"企业介绍";
    [arr1 addObject:model8];
    PublicCategoryElementModel *model9 = [[PublicCategoryElementModel alloc] init];
    model9.name = @"企业福利";
    [arr1 addObject:model9];
    [dataArray addObject:arr1];
    
    NSMutableArray *arr2 = [NSMutableArray arrayWithCapacity:0];
    PublicCategoryElementModel *model21 = [[PublicCategoryElementModel alloc] init];
    model21.name = @"企业座机";
    [arr2 addObject:model21];
    PublicCategoryElementModel *model22 = [[PublicCategoryElementModel alloc] init];
    model22.name = @"办公地址";
    [arr2 addObject:model22];
    PublicCategoryElementModel *model23 = [[PublicCategoryElementModel alloc] init];
    model23.name = @"企业网址";
    [arr2 addObject:model23];
    PublicCategoryElementModel *model24 = [[PublicCategoryElementModel alloc] init];
    model24.name = @"企业邮箱";
    [arr2 addObject:model24];
    [dataArray addObject:arr2];
}
- (void)headerBtnTap {
    picFlag = 1;
    headerPickerView.hidden = NO;
}
- (void)openCamera {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    if (picFlag == 1) {
        picker.allowsEditing = YES;
    } else {
        picker.allowsEditing = NO;
    }
    
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied) { // 无权限
        CustomView *alertView = [[CustomView alloc] initWithTitle:@"相机权限未开启" message:@"相机权限未开启，请进入系统【设置】>【隐私】>【相机】中打开开关,开启相机功能" prompt:nil sureBtn:@"确定" cancleBtn:@"取消" size:170];
        alertView.resultIndex = ^(NSInteger index) { //回调---处理一系列动作
            if (index == 2) {
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
            }
        };
        [alertView showXLAlertView];
    } else { // 有权限
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:picker animated:YES completion:nil];
    }
}
- (void)openPhotoLibrary {
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied) { // 无权限
        CustomView *alertView = [[CustomView alloc] initWithTitle:@"相册权限未开启" message:@"相册权限未开启，请进入系统【设置】>【隐私】>【相册】中打开开关,开启相册功能" prompt:nil sureBtn:@"确定" cancleBtn:@"取消" size:170];
        alertView.resultIndex = ^(NSInteger index) { //回调---处理一系列动作
            if (index == 2) {
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
            }
        };
        [alertView showXLAlertView];
    } else { // 有权限
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        if (picFlag == 1) {
            imagePicker.allowsEditing = YES;
        } else {
            imagePicker.allowsEditing = NO;
        }
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.delegate = self;
        imagePicker.navigationBar.translucent = NO;
        imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:imagePicker animated:YES completion:^{
            NSLog(@"打开相册");
        }];
    }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    NSLog(@"finish..");
    NSString *text = @"UIImagePickerControllerEditedImage";
    if (picFlag == 1) {
        text = @"UIImagePickerControllerEditedImage";
    } else {
        text = @"UIImagePickerControllerOriginalImage";
    }
    UIImage *image = [info objectForKey:text];
    SVPHUDShow;
    [DataToolsControl uploadImgType:((picFlag == 1)?UploadImgTypeOfCompanyLogo:UploadImgTypeOfCertificateImg) image:image successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            id data = responseObject[@"data"];
            if (self->picFlag == 1) {
                self->logoURL = data[@"url"];
                self->logoImgView.image = image;
            } else {
                self->cerURL = data[@"url"];
                [self->upCerBtn setBackgroundImage:image forState:UIControlStateNormal];
            }
            SVPHUDDismiss;
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(@"上传失败，请重新操作");
    }];
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)initUI {
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(WIDTH-50*Scale, HeightOfStatusBar+3*Scale, 50*Scale, 36) imageName:nil target:self action:@selector(okBtnTap) title:@"保存" titleColor:THEMECOLOR_OF_ORANGE];
    okBtn.titleLabel.font = Font(Font_15);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:okBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    headerView1 = [UIToolsControl initViewWithFrame:CGRectMake(0, 0, WIDTH, 130*Scale) backgroundColor:COLOR_OF_WHITE];
    UILabel *leftLbl = [UIToolsControl initLabelWithFrame:CGRectMake(LeftSpace, 0, WIDTH-30*Scale, 120*Scale) text:@"LOGO是企业文化的彰显，\n\n\n可提升企业的信用度" textColor:THEMECOLOR_OF_TEXT_666666 font:Font_13];
    leftLbl.numberOfLines = 0;
    leftLbl.lineBreakMode = NSLineBreakByWordWrapping;
    [headerView1 addSubview:leftLbl];
    logoImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(WIDTH-100*Scale, 15*Scale, 70*Scale, 70*Scale) imageName:@"logo_bg"];
    [headerView1 addSubview:logoImgView];
    UILabel *rightLbl = [UIToolsControl initLabelWithFrame:CGRectMake(WIDTH-100*Scale, EndY(logoImgView), 100*Scale, 30*Scale) text:@"上传企业LOGO" textColor:THEMECOLOR_OF_ORANGE font:Font_10];
    [headerView1 addSubview:rightLbl];
    UIView *view = [UIToolsControl initViewWithFrame:CGRectMake(0, 120*Scale, WIDTH, 10*Scale) backgroundColor:THEMECOLOR_OF_GRAY];
    [headerView1 addSubview:view];
    UIButton *headerBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, 0, WIDTH, 130*Scale) imageName:nil target:self action:@selector(headerBtnTap) title:nil titleColor:nil];
    [headerView1 addSubview:headerBtn];
    
    headerView2 = [UIToolsControl initViewWithFrame:CGRectMake(0, 0, WIDTH, 10*Scale) backgroundColor:THEMECOLOR_OF_GRAY];
    
    infoTableView = [UIToolsControl initTableViewWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height) target:self style:UITableViewStyleGrouped];
    infoTableView.rowHeight = 45*Scale;
    infoTableView.tableFooterView = [[UIView alloc] init];
    infoTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.view addSubview:infoTableView];
    
    footerView = [UIToolsControl initViewWithFrame:CGRectMake(0, 0, WIDTH, 220*Scale) backgroundColor:COLOR_OF_WHITE];
    UIView *lineView = [UIToolsControl initViewWithFrame:CGRectMake(0, 0, WIDTH, 10*Scale) backgroundColor:THEMECOLOR_OF_GRAY];
    [footerView addSubview:lineView];
    UILabel *topLbl = [UIToolsControl initLabelWithFrame:CGRectMake(LeftSpace, EndY(lineView), 200, 40*Scale) text:@"企业认证：" textColor:THEMECOLOR_OF_TEXT_333333 font:Font_13];
    [footerView addSubview:topLbl];
    upCerBtn = [UIToolsControl initButtonWithFrame:CGRectMake(LeftSpace, EndY(topLbl), 110*Scale, 110*Scale) imageName:nil target:self action:@selector(upCerBtnTap) title:nil titleColor:nil];
    [upCerBtn setBackgroundImage:[UIImage imageNamed:@"update_icon"] forState:UIControlStateNormal];
    [footerView addSubview:upCerBtn];
    
    UILabel *alertLbl = [UIToolsControl initLabelWithFrame:CGRectMake(LeftSpace, EndY(upCerBtn), WIDTH-30*Scale, 50*Scale) text:@"提示；上传企业营业执照，开启查看人才、发布职位的权限。" textColor:THEMECOLOR_OF_ORANGE font:Font_13];
    alertLbl.numberOfLines = 0;
    alertLbl.lineBreakMode = NSLineBreakByWordWrapping;
    [footerView addSubview:alertLbl];
}

@end
